const visitModel = require('@/models/visit.model')
const { apiGetIPAddress } = require('@/http')

const interceptor = async (req, _response, next) => {
  const ip = req.clientIp
  next()
  try {
    const data = await apiGetIPAddress(ip)
    const { province, city, isp, lonlat } = data
    const path = req._parsedOriginalUrl.pathname
    const method = req.method
    visitModel.add({
      ip,
      province,
      city,
      isp,
      lonlat,
      path,
      method
    })
  } catch (error) {
    console.log(error)
  }
}

module.exports = interceptor
